﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Modeling_Teacher_ListStudent : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WelcomeUser"] != null)
        {
            if (Session["EmployeeID"] != null && Session["Role"].ToString().Equals("EAS"))
            {
                UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

                if (udm.createConnection() != null && udm.showFeedback(udm.createConnection()) != null)
                {
                    gvAdmission.DataSource = udm.showFeedback(udm.createConnection());
                    if (!Page.IsPostBack)
                    {
                        gvAdmission.DataBind();
                    }
                }
                else
                {
                    Response.Redirect("~/ErrorPage.aspx");
                }
            }
            else
            {
                Response.Redirect("~/LoginMessage.aspx?id=1");
            }
        }
        else
        {
            Response.Redirect("~/Login.aspx?id=2");
        }
    }
    protected void gvAdmission_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvAdmission.EditIndex = e.NewEditIndex;
        gvAdmission.DataBind();

        DropDownList ddl = (DropDownList)gvAdmission.Rows[e.NewEditIndex].Cells[1].Controls[1];

        ddl.Items.Add(new ListItem("Not Check","0"));
        ddl.Items.Add(new ListItem("Checked", "1"));
    }
    protected void gvAdmission_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvAdmission.EditIndex = -1;
        gvAdmission.DataBind();
    }
    protected void gvAdmission_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = Convert.ToInt32(gvAdmission.Rows[e.RowIndex].Cells[2].Text.ToString());

        DropDownList ddl = (DropDownList)gvAdmission.Rows[e.RowIndex].Cells[1].Controls[1];

        int status = Convert.ToInt32(ddl.SelectedValue.ToString());

        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        if (status == 1)
        {
            if (udm.createConnection() != null)
            {
                if (udm.updateAdmission(udm.createConnection(), id, status))
                {
                    if (status == 1)
                    {
                        if (!udm.updateFeedbackChecked(udm.createConnection(), id))
                        {
                            Response.Redirect("~/ErrorPage.aspx");
                        }
                    } 
                }
                else
                {
                    Response.Redirect("~/ErrorPage.aspx");
                }

            }
            else
            {
                Response.Redirect("~/ErrorPage.aspx");
            } 
        }

        gvAdmission.EditIndex = -1;
        gvAdmission.DataSource = udm.showFeedback(udm.createConnection());
        gvAdmission.DataBind();
    }
}